/**
 * 
 */
package jmath.io;

import org.apache.poi.hssf.usermodel.*;
import jmath.function.*;
import cern.colt.matrix.DoubleFactory2D;
import cern.colt.matrix.DoubleMatrix2D;
import java.io.*;

/**
 * @author Jule
 * 
 */
public class XLSFileWorker extends IOFunction{

	public DoubleMatrix2D read(String fileName) {

		DoubleMatrix2D result = null;

		FileInputStream fileIn = null;
		try {
			fileIn = new FileInputStream(fileName);
			HSSFWorkbook wb = new HSSFWorkbook(fileIn);
			HSSFSheet sheet = wb.getSheetAt(0);
			result = DoubleFactory2D.dense.make(sheet.getLastRowNum() + 1,
					sheet.getRow(0).getLastCellNum() + 1);
			for (int row = 0; row < (sheet.getLastRowNum() + 1); row++) {
				HSSFRow hssfRow = sheet.getRow(row);
				for (int col = 0; col < sheet.getRow(0).getLastCellNum() + 1; col++) {
					HSSFCell cell = hssfRow.getCell((short) col);
					if (cell != null) {
						result.set(row, col, cell.getNumericCellValue());
					}
				}
			}
		} catch (Exception ex) {
			jMathConsole.warn(ioUtils.getMessage("CANNOT_READ_FILE"));
		} finally {
			try {
				if (fileIn != null) {
					fileIn.close();
				}
			} catch (IOException ex) {
			}
		}
		return result;

	}
	

	public void write(DoubleMatrix2D data, String fileName) {
		if (data == null || data.rows() == 0 || data.columns() == 0) {
			jMathConsole.warn("write(DoubleMatrix2D array,String fileName)-- matrix2D must be of n x n dimention");
			return;
		}
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("data");

		for (int i = 0; i < data.rows(); i++) {
			HSSFRow row = sheet.createRow((short) i);
			for (int j = 0; j < data.columns(); j++) {
				row.createCell((short) j).setCellValue(data.get(i, j));
			}

		}

		FileOutputStream fileOut = null;
		try {
			fileOut = new FileOutputStream(fileName);
			wb.write(fileOut);
		} catch (IOException ex) {
			jMathConsole.warn(ioUtils.getMessage("ERROR_WHILE_SAVE"));
		} finally {
			try {
				if (fileOut != null) {
					fileOut.close();
				}
			} catch (IOException ex) {
			}
		}

	}

	public void write(double[][] data, String fileName) {
		if (data == null || data.length == 0 || data[0].length == 0) {
			jMathConsole.warn("write(double[][] array,String fileName)-- matrix2D must be of n x n dimention");
			return;
		}
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("data");

		int i = 0;
		for (double[] subArray : data) {
			HSSFRow row = sheet.createRow((short) i);
			int j = 0;
			for (double value : subArray) {
				row.createCell((short) j).setCellValue(value);
				j = j + 1;
			}
			i = i + 1;
		}

		FileOutputStream fileOut = null;
		try {
			fileOut = new FileOutputStream(fileName);
			wb.write(fileOut);
		} catch (IOException ex) {
			jMathConsole.warn(ioUtils.getMessage("ERROR_WHILE_SAVE"));
		} finally {
			try {
				if (fileOut != null) {
					fileOut.close();
				}
			} catch (IOException ex) {
			}
		}

	}	

}
